package com.molichuxing.services.infrastructure.service.impl;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.services.infrastructure.bean.OwnershipBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.OwnershipMapper;
import com.molichuxing.services.infrastructure.dto.request.create.OwnershipCreateDto;
import com.molichuxing.services.infrastructure.dto.request.create.OwnershipModifyDto;
import com.molichuxing.services.infrastructure.dto.response.OwnershipDto;
import com.molichuxing.services.infrastructure.service.OwnershipService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产权方，供签订合同使用
 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-08-13
 */
@Service("ownershipService")
public class OwnershipServiceImpl implements OwnershipService {

  @Resource
  private OwnershipMapper  ownershipMapper;

  @Override
  public OwnershipDto getOwnershipById(Integer id) {
    OwnershipBean ownershipBean = ownershipMapper.getById(id);
    return Convert.toOwnershipBeanDto(ownershipBean);
  }

  @Override
  public OwnershipDto getOwnershipByName(String name) {
    OwnershipBean ownershipBean = ownershipMapper.getByName(name);
    return Convert.toOwnershipBeanDto(ownershipBean);
  }

  @Override
  public Paged<OwnershipDto> getOwnershipListPage(Integer pageSize, Integer pageNum) {
    int count = ownershipMapper.getOwnershipPageCount();
    if(count <= 0) {
      return new Paged<>();
    }
    List<OwnershipBean> pageResult = ownershipMapper.getOwnershipPageList( pageNum, pageSize);
    return new Paged<>(Convert.toOwnershipPageBeanDto(pageResult), count, pageSize );
  }

  @Override
  public int createOwnership(OwnershipCreateDto ownershipCreateDto) {
    OwnershipBean ownershipBean = new OwnershipBean();
    BeanUtils.copyProperties(ownershipCreateDto, ownershipBean);
    ownershipBean.setGmtCreate(LocalDateTime.now());
    return ownershipMapper.createOwnership(ownershipBean);
  }

  @Override
  public int modifyOwnership(OwnershipModifyDto ownershipModifyDto) {
    OwnershipBean ownershipBean = new OwnershipBean();
    BeanUtils.copyProperties(ownershipModifyDto, ownershipBean);
    ownershipBean.setId(ownershipModifyDto.getId());
    return ownershipMapper.modifyOwnership(ownershipBean);
  }

  @Override
  public Map<Integer, OwnershipDto> getOwnershipListByIds(List<Integer> ids) {
    Map<Integer, OwnershipDto> dtoMap = new HashMap<>();
    if (ids!=null && !ids.isEmpty()){
      List<OwnershipBean> dtoList = ownershipMapper.selectOwnershipsByIds(ids);
      if (dtoList!=null && !dtoList.isEmpty()){
        for (int i=0; i<ids.size(); i++){
          for (OwnershipBean c:dtoList){
            if (ids.get(i)==(c.getId())){
              dtoMap.put(ids.get(i), Convert.toOwnershipBeanDto(c));
            }
          }
        }

      }
    }
    return dtoMap;
  }
}
